package com.liang.handler;

import cn.hutool.json.JSONUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/5/12
 * Time: 21:16
 * Description: No Description
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    // 权限校验未通过的响应数据
    @Override
    public void handle(HttpServletRequest req, HttpServletResponse resp, AccessDeniedException e) throws IOException, ServletException {


        BaseResponse.response(req,resp,e,100013,"您当前没有访问权限");

//        // 1. 设置响应的数据格式
//        resp.setContentType("application/json;charset=utf-8");
//        // 2. 响应状态码
//        resp.setStatus(HttpStatus.OK.value());
//        // 3. 设置响应数据
//        PrintWriter writer = resp.getWriter();
//        Map<String,Object> resultMap = new HashMap<>();
//        // 系统中的状态标识, 使用httpStatus 状态码很容易,产生 混淆 不方便调试错误
//        resultMap.put("code",100013);
//        resultMap.put("msg","您当前没有访问权限");
//        // 把响应数据转成 json
//        String resultJson = JSONUtil.toJsonStr(resultMap);
//        writer.print(resultJson);
//        writer.flush();
//        writer.close();
    }
}
